<?php

/*
    Function: phptal_tales_validation_error
    PHPTAL Tales to check for a validation error in Codeigniter
    
    Usage:
        Simply use it as any other tale
        > <span tal:condition="validation-error:input_name">There was an error</span>
        It is also posible to specify an alterative return. For example:
        > <input type="text" name="input_name" tal:attributes="class validation-error:input_name[error]" />
        Here the tale is *validation-error:input_name[error]* the `class`
        attribute of the input element would then be `error` if there was a
        validation error
*/
function phptal_tales_validation_error($src, $nothrow) {
    
    $_ci =& get_instance();
    
    preg_match("/([^\[]+)(?:\[([\w-]+)\])?/", $src, $m);
    
    $validation_field = $m[1] == 'error_string' ? $m[1] : $m[1].'_error';
    $output_string = isset($m[2]) ? $m[2] : null;
    
    $true_output = $output_string ? PHPTAL_TalesInternal::string($output_string) : 'true';
    $val_var = '$GLOBALS[\'_ci\']->validation->{'.PHPTAL_TalesInternal::string($validation_field).'}';
    $r = "(isset($val_var) && !empty($val_var)) ? $true_output : false";
    
    return $r;
}

?>